<template>
  <div class="page-class">
    <Breadcrumb>
      <a-button type="primary" @click="handleAdd" v-auth="'topic.add'"
        >新增</a-button
      >
    </Breadcrumb>
    <div class="cnt-class">
      <div class="query">
        <a-row>
          <a-col :span="6">
            <a-input-search
              v-model.trim="topicName"
              :maxLength="50"
              allowClear
              placeholder="请输入主题名称"
              @search="onSearch"
            >
              <a-button slot="enterButton" type="primary"> 搜索 </a-button>
            </a-input-search>
          </a-col>
        </a-row>
      </div>
      <div class="table">
        <BaseTable
          ref="table"
          rowKey="id"
          :scroll="{ y: 540 }"
          :loading="loading"
          :columns="columns"
          :dataSource="list"
          :hideSelect="false"
          :selectedRowKeys="selectedRowKeys"
          @deleteRows="handleDelete"
          @selectChange="selectChange"
          @change="getList"
        ></BaseTable>
      </div>
    </div>
  </div>
</template>
<script>
import BaseTable from "@/components/BaseTable";
import httpApi from "@/api/log";

export default {
  components: {
    BaseTable
  },
  data() {
    return {
      loading: false,
      selectedRowKeys: [],
      time: -1,
      topicName: undefined,
      list: [],
      columns: [
        {
          title: "主题类型名称",
          dataIndex: "topicName",
          ellipsis: true
        },
        {
          title: "评论数",
          dataIndex: "number",
          ellipsis: true,
          customRender: (data) => {
            return data || 0 + " 个 ";
          }
        },
        {
          title: "状态",
          dataIndex: "state",
          ellipsis: true,
          customRender: (data) => {
            return data;
          }
        },
        {
          title: "操作",
          eclipse: true,
          customRender: (v, row) => {
            return (
              <div class="operate-td">
                <a
                  onClick={() => this.handleState(row, row.state === 1 ? 0 : 1)}
                  v-auth="topic.changState"
                >
                  {row.state === 1 ? "禁用" : "启用"}
                </a>
                <div v-auth="topic.delete">
                  <a-divider type="vertical" />
                  <a onClick={() => this.handleDelete(row)}>删除</a>
                </div>
              </div>
            );
          }
        }
      ]
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    handleAdd() {
      //新增
    },
    onSearch() {
      clearTimeout(this.time);
      this.time = setTimeout(() => {
        this.getList(1);
      }, 600);
    },
    getList(current) {
      this.loading = true;
      if (current === 1) {
        this.$refs.table.pageInfo.current = 1;
      }
      httpApi
        .getList({
          topicName: this.topicName,
          pageNo: this.$refs.table.pageInfo.current,
          pageSize: this.$refs.table.pageInfo.pageSize
        })
        .then((res) => {
          if (res.retcode === 0) {
            this.list = res.data.records || [];
            if (this.$refs.table) {
              this.$refs.table.pageInfo.total = res.data.total;
            }
          } else {
            this.$message.error(res.msg);
          }
          this.loading = false;
        })
        .catch((err) => {
          this.$message.error(err.msg);
          this.loading = false;
        });
    },
    handleDelete(row) {
      let title = "";
      let content = "";
      let ids = [];
      if (row) {
        ids = [row.id];
        title = "删除：";
        content = "是否删除该主题，删除后将自动清除该类型的所有评论？";
      } else {
        ids = this.selectedRowKeys;
        title = "批量删除：";
        content = `是否删除已选中的 ${ids.length} 个主题，删除后将自动清除该类型的所有评论？`;
      }

      this.$confirm({
        title,
        content,
        onOk: () => {
          httpApi
            .deleteRows(ids)
            .then((res) => {
              if (res.retcode === 0) {
                this.$message.success("删除成功！");
                this.getList();
              } else {
                this.$message.error(res.msg || "删除失败！");
              }
            })
            .catch((err) => {
              this.$message.error(err.msg || "删除失败！");
            });
        },
        onCancel: () => {
          console.log("已取消删除操作");
        }
      });
    },
    handleState(row, type) {
      let title = "";
      let content = "";
      if (type === 0) {
        title = "禁用：";
        content =
          "是否禁用该主题，禁用后不可发布该类型评论且隐藏已发布的评论？";
      } else {
        title = "启用：";
        content = `是否启用该主题？`;
      }

      this.$confirm({
        title,
        content,
        onOk: () => {
          httpApi
            .deleteRows({
              id: row.id
            })
            .then((res) => {
              if (res.retcode === 0) {
                this.$message.success("操作成功！");
                this.getList();
              } else {
                this.$message.error(res.msg || "操作失败！");
              }
            })
            .catch((err) => {
              this.$message.error(err.msg || "操作失败！");
            });
        },
        onCancel: () => {
          console.log("已取消操作");
        }
      });
    },
    selectChange(selectedKeys) {
      this.selectedRowKeys = selectedKeys;
    }
  }
};
</script>
